﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_Buy : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!_User.isSurrogate)
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);

            return;
        }

        if (!this.IsPostBack)
        {
            labContent.Text = PublicFunction.GetOptionsBin("注册协议");

            FillddlBank();

            long ParentID = ShoveEL._Convert.StrToLong(this.Request["PID"], -1);
            int LR = ShoveEL._Convert.StrToInt(this.Request["LR"], -1);

            //2008.04.21  添加1.满足消费ID自动生成：TT+八位随机数字
            //                2.密码默认:888888

            System.Random r = new Random();
            //tbUserName.Text = "HS" + r.Next(100000, 999999).ToString();

            if (ParentID >= 0)
            {
                User tu = new User();
                tu.ID = ParentID;

                string Return = "";
                int Result = tu.GetUserInfoByID(ref Return);
                if (Result == 0)
                {
                    tbParentUserID.Text = ParentID.ToString();
                    tbParentUserName.Text = tu.Name;
                    tbCommendUserName.Text = _User.Name;

                    tbParentUserName.Enabled = false;                    
                }
            }            

            if ((LR == 1) || (LR == 2))
            {
                if (LR == 1)
                {
                    rbL.Checked = true;
                    rbR.Checked = false;
                }

                if (LR == 2)
                {
                    rbL.Checked = false;
                    rbR.Checked = true;
                }
                rbL.Enabled = false;
                rbR.Enabled = false;
            }

            FillProvince();
            tbUserBirthday.Value = "1970-01-01";
            btnBuy.Attributes.Add("onclick", "return confirm('" + System.Web.HttpUtility.HtmlEncode("您确信输入的资料无误，并且立即注册吗？") + "');");
        }

        //this.tbUserPassword.Text = "888888";
        //this.tbUserPassword_2.Text = "888888";
        //this.tbUserPassword2.Text = "888888";
        //this.tbUserPassword2_2.Text = "888888";

        tbUserPassword.Attributes.Add("value", tbUserPassword.Text);
        tbUserPassword_2.Attributes.Add("value", tbUserPassword_2.Text);
        tbUserPassword2.Attributes.Add("value", tbUserPassword2.Text);
        tbUserPassword2_2.Attributes.Add("value", tbUserPassword2_2.Text);
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = false;
        RequestLoginPage = "BalanceCenter/Buy.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
    }

    private void FillProvince()
    {
        DataTable dt = DAL.Tables.T_Province.Select("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlProvince, dt, "Name", "id");

        if (ddlProvince.Items.Count > 0)
        {
            ddlProvince.SelectedIndex = 0;
            FillCity(int.Parse(ddlProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = DAL.Tables.T_City.Select("", "ProvinceID = " + ProvinceID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlCity, dt, "Name", "id");


        if (ddlCity.Items.Count > 0)
        {
            ddlCity.SelectedIndex = 0;
        }
    }

    protected void ddlProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlProvince.SelectedValue));
    }

    private void FillddlBank()
    {
        DataTable dt = DAL.Tables.T_Banks.Select("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlBankName, dt, "Name", "ID");
    }

    protected void btnCheckUserName_Click(object sender, EventArgs e)
    {
        DataTable dt = DAL.Tables.T_Users.Select("", "[Name] = '" + ShoveEL._Web.Utility.FilteSqlInfusion(tbUserName.Text.Trim()) + "'", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (dt.Rows.Count == 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "此用户名可以使用!");

            return;
        }
        else
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "该用户名已经存在，请用其他用户名注册!");

            return;
        }
    }

    protected void btnBuy_Click(object sender, EventArgs e)
    {
        if (tbCommendUserName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入推荐人消费ID。");
            return;
        }

        if (tbParentUserName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入网络位置上级消费ID。");
            return;
        }

        if (tbUserName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入消费ID。");
            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入消费ID真实姓名。");
            return;
        }

        if (tbUserPassword.Text == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入一级密码。");
            return;
        }

        if (tbUserPassword_2.Text != tbUserPassword.Text)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "输入的两次一级密码不一致。");
            return;
        }

        if (tbUserPassword2.Text == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入二级密码。");
            return;
        }

        if (tbUserPassword2_2.Text != tbUserPassword2.Text)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "输入的两次二级密码不一致。");
            return;
        }

        if (tbUserIDCardNumber.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入身份证号。");
            return;
        }
        //else
        //{
        //    if (GetUsersByCardNumber(tbUserIDCardNumber.Text.Trim()) == 3)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "该身份证号已注册了三个会员。");
        //        return;
        //    }
        //}

        /*
                if (tbUserEmail.Text.Trim() == "")
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入电子邮件地址。");
                    return;
                }
       
                if (tbUserTelephone.Text.Trim() == "")
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入联系电话。");
                    return;
                }
        */

        if (tbUserMobile.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入手机号码。");
            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入详细开户行名称。");
            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");
            return;
        }

        if (tbUserBankCardName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入很行卡的帐户名。");
            return;
        }
        

        string Return = "";
        int Result = _User.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (_User.VirtualMoney< ShoveEL._Convert.StrToDouble(DAL.Functions.F_GetOptions.Call("每单金额"), 0))
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "对不起，您帐户余额不足。");

            return;
        }

        int UserType = int.Parse(ddlUserType.SelectedValue);
        double Money = 0;
        switch (UserType)
        {
            case 1:
                Money = PublicFunction.GetOptionsAsDouble("每单价格", 0);
                break;
            case 2:
                Money = PublicFunction.GetOptionsAsDouble("3单价格", 0);
                break;
            case 3:
                Money = PublicFunction.GetOptionsAsDouble("5单价格", 0);
                break;
            case 4:
                Money = PublicFunction.GetOptionsAsDouble("10单价格", 0);
                break;   
        }

        if (Money <= 0)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "购买金额错误", this.GetType().BaseType.FullName);

            return;
        }

        //if (cbisActivation.Checked == true)
        //{
        //    if (_User.VirtualMoney < Money)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "对不起，您帐户余额不足。");

        //        return;
        //    }
        //}

        User Commend = new User();
        Commend.Name = tbCommendUserName.Text.Trim();

        Return = "";
        Result = Commend.GetUserInfoByName(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "推荐人消费ID不存在。");

            return;
        }

        User Parent = new User();
        Parent.Name = tbParentUserName.Text.Trim();
        Return = "";
        Result = Parent.GetUserInfoByName(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "网络位置上级消费ID不存在。");

            return;
        }

        int LR = 0;
        if (this.rbL.Checked == true)
        {
            LR = 1;
        }
        else if (this.rbR.Checked == true)
        {
            LR = 2;
        }

        if (!PublicFunction.CheckSystemRegisterState())
        {
            if (DAL.Tables.T_Users.GetCount("") >= 8)
            {
                PublicFunction.GoError(ErrorNumber.Unknow, "对不起您的系统未激活，不能继续报单", this.GetType().BaseType.FullName);

                return;
            }
        }

        System.Threading.Thread.Sleep(200);

        Result = 0;
        string ReturnDescription = "";

        int i = DAL.Procedures.P_Buy.Call(_User.ID, ShoveEL._Convert.ToDBC(tbUserName.Text.Trim()), ShoveEL._Convert.ToDBC(tbUserRealityName.Text.Trim()), PublicFunction.EncryptPassword(tbUserPassword.Text), PublicFunction.EncryptPassword(tbUserPassword2.Text),
            rbUserSexM.Checked ? "男" : "女", tbUserBirthday.Value, int.Parse(ddlCity.SelectedValue), ShoveEL._Convert.ToDBC(tbUserIDCardNumber.Text.Trim()), ShoveEL._Convert.ToDBC(tbUserAddress.Text.Trim()), ShoveEL._Convert.ToDBC(tbUserEmail.Text.Trim()),
            ShoveEL._Convert.ToDBC(tbUserQQ.Text.Trim()), ShoveEL._Convert.ToDBC(tbUserTelephone.Text.Trim()), ShoveEL._Convert.ToDBC(tbUserMobile.Text.Trim()), ShoveEL._Convert.StrToShort(ddlBankName.SelectedValue, 1), tbUserBankNameDetail.Text.Trim(),
            ShoveEL._Convert.ToDBC(tbUserBankCardNumber.Text.Trim()),tbUserBankCardName.Text.Trim(), UserType, Parent.ID, LR, Commend.ID, cbAutoNext.Checked, false, ref Result, ref ReturnDescription);           
        if (i < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);

            return;
        }
        else
        {
            hUserID.Value = Result.ToString();      //保存新加入会员的ID号
        }

        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
    }

    /// <summary>
    /// 查询出该身份证号码所注册过的会员
    /// </summary>
    private int GetUsersByCardNumber(string CardNumber)
    {
        int Amount = 0;
        SqlConnection conn = ShoveEL.Database.MSSQL.CreateDataConnection();

        if (conn == null)
        {
            this.Response.Redirect("Error.aspx?ErrorNumber=01", true);
            return Amount;
        }

        SqlCommand Cmd = new SqlCommand("select count(*)as Amount from T_Users where IDCardNumber = '" + CardNumber + "'", conn);
        SqlDataReader dr = null;
        try
        {
            dr = Cmd.ExecuteReader();
        }
        catch
        {
            conn.Close();
            this.Response.Redirect("Error.aspx?ErrorNumber=01", true);
            return Amount;
        }

        while (dr.Read())
        {
            Amount = ShoveEL._Convert.StrToInt(dr["Amount"].ToString(), 0);
        }

        return Amount;
    }

    //激活此会员
    protected void btnActiveUser_Click(object sender, EventArgs e)
    {
        long UserID = ShoveEL._Convert.StrToLong(hUserID.Value, 0);

        string Return = "";
        int Result = 0;

        if (DAL.Procedures.P_ActivationUser.Call(UserID, _User.ID, ref Result, ref Return) < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        ShoveEL._Web.JavaScript.Alert(this.Page, "激活成功！", "Net.aspx?ID=" + UserID);        
    }

    //回到组织结构图中
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("NetCopy.aspx");
    }
}
